package com.eati.cm.auth;

import java.io.InvalidObjectException;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import com.eati.cm.excp.ThirdPartyAccountUnboundExcp;
import com.eati.cm.model.Login;
import com.eati.cm.model.ThirdPartyAccount;
import com.eati.cm.serv.ILoginServ;
import com.eati.cm.serv.IThirdPartyAccountServ;
import com.eati.cm.serv.IUserServ;
import com.eati.cm.serv.dispacher.DispMan;
import com.eati.cm.serv.dispacher.LoginDisp;
import com.eati.cm.utils.CastUtil;

public class CmUserAuthProvider implements AuthenticationProvider {

    private ILoginServ loginServ;
    public CmUserAuthProvider(ILoginServ loginServ) {
        this.loginServ = loginServ;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    	System.out.println("AuthenticationProvider::authenticate!!");
        /*String usrId = (String)authentication.getPrincipal();
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(usrId);
        if(userDetails == null){
            return null;
        }*/
    	
    	if(!(authentication instanceof ThirdpartyAuthCBToken)) {
    		new InvalidObjectException("CmUserAuthProvider can only process ThiedpartyAuthCBToken!").printStackTrace();
    		return null;
    	}
    	
    	LoginOnlineToken lotoken = loginServ.login((ThirdpartyAuthCBToken) authentication);
    	return lotoken;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.getName().equals(ThirdpartyAuthCBToken.class.getName());
    }
}
